package rm2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import data.ItemList;

public class DRSserverinit 
{	
	public /*static*/ Map<String, ItemList> slist = new HashMap<String, ItemList>();

	public  String input_folder_path;
	public  String server;
	public  String ip_address;
	public  int udp_port;
	public  int rmi_port;
	public  String closest_1_server_address;
	public  int closest_1_server_udp_port;
	public  int closest_1_server_rmi_port;
	public  String closest_2_server_address;
	public  int closest_2_server_udp_port;
	public  int closest_2_server_rmi_port;
	
	DRSserverinit(String path)
	{
		//this.initItemList("input" + File.separator + "ItemList");
		this.input_folder_path = path;
		this.initItemList(input_folder_path + File.separator + "ItemList");
		this.initConfiguration(input_folder_path + File.separator + "server.conf");
	}
	
	public void initItemList(String fileName)
	{
		try{
			
			File file = new File(fileName);			
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String itemID = "";			
			int capacity = 0;			
			String line;
			
			while ( (line = br.readLine()) != null)
			{
				int index = 0;
				StringTokenizer st = new StringTokenizer(line, " ");
				while(st.hasMoreTokens()){
					index++;
					String token = st.nextToken().trim();
					
					// ID
					if (index==1){
						itemID = token;						
						continue;
					}
					
					// capacity 
					if (index==2){
						capacity = Integer.parseInt(token);		
						ItemList item_list = new ItemList(itemID, capacity,this);
						this.slist.put(item_list.getItem(), item_list);
						//System.out.println(slist.size());
						
						continue;
					}					
				}
			}	// end of while
			
		} catch (IOException e) {System.out.println(e);}
			

	}
	
	public void initConfiguration(String file_path)
	{
		String line;
		StringTokenizer st;
		String token;
		
		try
		{
			File file = new File(file_path);			
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			while ( (line = br.readLine()) != null)
			{
				st = new StringTokenizer(line, ":");
				token = st.nextToken();
				
				if(token.equalsIgnoreCase("server"))
					server = st.nextToken().trim();
				else if (token.equalsIgnoreCase("ip address"))
					ip_address = st.nextToken().trim();
				else if (token.equalsIgnoreCase("UDP Port"))
					udp_port = Integer.parseInt(st.nextToken().trim());
				else if (token.equalsIgnoreCase("RMI Port"))
					rmi_port = Integer.parseInt(st.nextToken().trim());
				else if (token.equalsIgnoreCase("closest-1 server address"))
					closest_1_server_address = st.nextToken().trim();
				else if (token.equalsIgnoreCase("closest-1 server UDP port"))
					closest_1_server_udp_port = Integer.parseInt(st.nextToken().trim());
				else if (token.equalsIgnoreCase("closest-1 server RMI port"))
					closest_1_server_rmi_port = Integer.parseInt(st.nextToken().trim());
				else if (token.equalsIgnoreCase("closest-2 server address"))
					closest_2_server_address = st.nextToken().trim();
				else if (token.equalsIgnoreCase("closest-2 server UDP port"))
					closest_2_server_udp_port = Integer.parseInt(st.nextToken().trim());
				else if (token.equalsIgnoreCase("closest-2 server RMI port"))
					closest_2_server_rmi_port = Integer.parseInt(st.nextToken().trim());
				
			}
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
		
	}

}
